package com.ocl.www.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.ocl.www.domain.Article;
import com.ocl.www.service.ArticleService;

/**
 * 服务启动执行
 *
 * @author   单红宇(365384722)
 * @myblog  http://blog.csdn.net/catoop/
 * @create    2016年1月9日
 */
@Component
@Order(value=1)
public class StartupOfLoad implements CommandLineRunner {

	
	@Autowired
	private HttpSolrClient solrClient;
	@Autowired
	private ArticleService articleService;

	@Autowired
	private EhCacheManager em;

	private Logger log = Logger.getLogger(StartupOfLoad.class);
	
	@Override
    public void run(String... args) throws Exception {
		//createArticleIndex();
		//远程调用接口返回值data
//		String ttargetURL = "http://192.168.1.39/cas-server-webapp/userInter";
//
//		String tinput = "mobilePhone=13020224068&youname=calldbell1callbell1";
//		String tdata = OutServerUntils.CallInterface(ttargetURL,tinput,"");
//		System.out.println("AAAAAAAAAAAAAAAAAAAAA");
		selectAllArticleOrderByLikeCount();
    }

	/**
	 * 加载文章索引到solr
	 */
	public  void createArticleIndex() {
		// TODO Auto-generated method stub
		List<Article> listart = null;
		try {
			listart = articleService.selectAllArticle();
		} catch (Exception e) {
			e.printStackTrace();
		}

		if(!listart.isEmpty()){
		
			int i = 0;
	        List<SolrInputDocument> docList = new ArrayList<SolrInputDocument>();
	        
	        for(Article obj : listart){
	        	
	            SolrInputDocument doc = new SolrInputDocument();
				log.info(i+"文章内容示例:"+obj.getArticlecontent());
	            doc.addField("id",i++);
				doc.addField("articleid",obj.getArticleId());
	            doc.addField("title", obj.getTitle());
	            doc.addField("articlecontent",obj.getArticlecontent());
				doc.addField("tagcontent",obj.getTagcontent());
	            docList.add(doc);
	        }
	        try {
	        	solrClient.add(docList);
	        	solrClient.commit();
	        } catch (SolrServerException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
    	
		}
//        this. queryDocs();
	}


//	public void setStartupEm(){
//
//		em.getCache("shiroCache").put("hasLetter","true");
//	}
	/**
	 * 查询热门文章 ，并缓存到em中；
	 * @return
	 */
	public void selectAllArticleOrderByLikeCount(){
		//List<Article> list = new ArrayList<Article>();
		try {
//			list =
			em.getCache("shiroCache").put("hotArticle",articleService.selectAllArticleOrderByLikeCount());
			log.info("========= data of hotArticle already save in em ===============");
		} catch (Exception e) {
			log.info(e.getMessage());
		}
		//、、return list;

	}

	
	public void queryDocs() {
        SolrQuery query = new SolrQuery();
        query.setQuery("title:贝尔");
        query.setQuery("articlecontent:('胶囊'AND'飞机')");
        QueryResponse response = null;
        try {
            response = solrClient.query(query);
            System.out.println(response.toString());
            System.out.println();
            SolrDocumentList docs = response.getResults();
            System.out.println(response.getResults().size());
            System.out.println("文档个数：" + docs.getNumFound());
            System.out.println("查询时间：" + response.getQTime());
            for (SolrDocument doc : docs) {
                System.out.println("title: " + doc.getFieldValue("title"));
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
	
    

}